﻿using System;
using System.Collections.Generic;
using CNDreams.Entity;
using CNDreams.Web.Base;
using Project.Common;
using Project.Entity.Base;
using Project.Web.Base.Utility;
using CNDreams.Enum;
using System.Web;
using CNDreams.ServiceInterface;
using System.Linq.Expressions;
using Serialize.Linq.Extensions;
namespace CNDreams.Web.Areas.ModuleSys.Models
{
	/// <summary>
	/// 部门管理Index Model
	/// 作者：尤啸
	/// 时间：2012-06-26
	/// </summary>
	public class ModelSysOrgDepartmentManageIndex : Model
	{
		private IServiceUTOrgDepartment bizUTOrgDepartment;
		private BaseSearch searchEntity;
		public BaseSearch SearchEntity
		{
			get
			{
				if (searchEntity == null)
					searchEntity = new BaseSearch();
				if (string.IsNullOrEmpty(searchEntity._OrderName))
				{
					searchEntity._OrderName = ReflectionTools.GetPropertyNameFromExpression<UTOrgDepartment>(x => x.InputDate);
					searchEntity._OrderDirection = ((int)EnumOrder.ASC).ToString();
				}
				return searchEntity;
			}
			set { searchEntity = value; }
		}
		private Guid? organizationId;
		/// <summary>
		/// 机构ID
		/// </summary>
		public Guid? OrganizationId
		{
			get { return organizationId; }
			set { organizationId = value; }
		}
		private IList<UTOrgDepartment> treeDataSources;

		/// <summary>
		/// 部门树
		/// </summary>
		public IList<UTOrgDepartment> TreeDataSources
		{
			get
			{
				if (treeDataSources != null)
					return treeDataSources;
				var tempExpression = (Expression<Func<UTOrgDepartment, object>>)(x => x.OrderIndex);
				treeDataSources = bizUTOrgDepartment.List(tempExpression.ToExpressionNode(), EnumOrder.ASC);
				return treeDataSources;
			}
		}
		/// <summary>
		/// 部门列表
		/// </summary>
		public IList<UTOrgDepartment> GridDataSources { get; set; }

		public void RetriveData()
		{
			ExpressionCondition<UTOrgDepartment> condition = ExpressionCondition<UTOrgDepartment>.GetInstance();
			if (!string.IsNullOrEmpty(SearchEntity._CommonSearchCondition))
			{
				condition.Or(x => x.DepartmentName, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like).
					 Or(x => x.DutyUser, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like).
					 Or(x => x.Email, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like).
					 Or(x => x.Mobile, SearchEntity._CommonSearchCondition, ExpressionValueRelation.Like);
			}
			else if (OrganizationId != null && string.IsNullOrEmpty(SearchEntity._CommonSearchCondition))
			{
				condition.And(x => x.OrganizationID, OrganizationId, ExpressionValueRelation.Equal);

			}

			GridDataSources = bizUTOrgDepartment.PaginateListBy(SearchEntity._PageSize.Value, SearchEntity._PageIndex.Value, ref Count, condition.ConditionExpression.ToExpressionNode(), SearchEntity._OrderName, SearchEntity.EnumOrderDirection);
			PaginateHelperObject = PaginateHelper.ConstructPaginate(SearchEntity._PageSize.Value, Count, SearchEntity._PageIndex.Value, SearchEntity, "SearchEntity", HttpContext.Current.Request.RawUrl);
		}

		/// <summary>
		/// 保存或新增部门
		/// </summary>
		/// <param name="departmentEntity"></param>
		public void Save(UTOrgDepartment departmentEntity)
		{
			//新增
			if (departmentEntity.ID == Guid.Empty)
			{
				departmentEntity.InputDate = DateTime.Now;
				departmentEntity.InputUser = SessionManager.CurrentSysUser.ID;

				bizUTOrgDepartment.SaveOrUpdate(departmentEntity);
			}//修改
			else
			{
				UTOrgDepartment uTOrgDepartment = bizUTOrgDepartment.GetFirst(ReflectionTools.SerializeExpression<UTOrgDepartment>(x => x.ID == departmentEntity.ID));
				uTOrgDepartment.DepartmentName = departmentEntity.DepartmentName;
				uTOrgDepartment.DutyUser = departmentEntity.DutyUser;
				uTOrgDepartment.Mobile = departmentEntity.Mobile;
				uTOrgDepartment.Email = departmentEntity.Email;
				uTOrgDepartment.Remark = departmentEntity.Remark;

				uTOrgDepartment.UpdateDate = DateTime.Now;
				uTOrgDepartment.UpdateUser = SessionManager.CurrentSysUser.ID;
				bizUTOrgDepartment.SaveOrUpdate(uTOrgDepartment);
			}

		}


	}


	/// <summary> 
	/// 用户 新增 修改  model create
	/// 作者：尤啸
	/// 时间：2012-06-26
	/// </summary>
	public class ModelSysOrgDepartmentManageCreate : Model
	{
		private IServiceUTOrgDepartment bizUTOrgDepartment;
		private Guid? organizationId;
		/// <summary>
		/// 组织机构ID
		/// </summary>
		public Guid? OrganizationId
		{
			get { return organizationId; }
			set { organizationId = value; }
		}

		/// <summary>
		/// 所有部门列表
		/// </summary>
		public IList<UTOrgDepartment> UTOrgDepartmentList { get; set; }

		private UTOrgDepartment uTOrgDepartmentEntity = new UTOrgDepartment();
		/// <summary>
		/// 部门用户实体
		/// </summary>
		public UTOrgDepartment UTOrgDepartmentEntity
		{
			get { return uTOrgDepartmentEntity; }
			set { uTOrgDepartmentEntity = value; }
		}
		/// <summary>
		/// 页面状态
		/// </summary>
		public EnumPageState? PageState { get; set; }



		public void RetriveData()
		{

			var tempExpression1 = (Expression<Func<UTOrgDepartment, object>>)(x => x.ID);

			//查找所有部门
			UTOrgDepartmentList = bizUTOrgDepartment.List(tempExpression1.ToExpressionNode(), EnumOrder.ASC);
			if (organizationId != null)
			{
				var tempExpression2 = (Expression<Func<UTOrgDepartment, bool>>)(x => x.ID == organizationId);
				uTOrgDepartmentEntity = bizUTOrgDepartment.GetFirst(tempExpression2.ToExpressionNode());
			}


		}

		/// <summary>
		/// 保存新增或修改用户信息
		/// </summary>
		public void Save()
		{
			//如果是新增
			if (UTOrgDepartmentEntity.ID == Guid.Empty && organizationId != Guid.Empty)
			{
				UTOrgDepartmentEntity.InputDate = DateTime.Now;
				UTOrgDepartmentEntity.InputUser = SessionManager.CurrentSysUser.ID;
				UTOrgDepartmentEntity.OrganizationID = organizationId;
				bizUTOrgDepartment.Save(UTOrgDepartmentEntity);

			}//修改 
			else if (UTOrgDepartmentEntity.ID != Guid.Empty)
			{
				var tempExpression = (Expression<Func<UTOrgDepartment, bool>>)(x => x.ID == UTOrgDepartmentEntity.ID);
				//取出源数据 进行修改
				UTOrgDepartment uTOrgDepartment = bizUTOrgDepartment.GetFirst(tempExpression.ToExpressionNode());
				uTOrgDepartment.DepartmentName = UTOrgDepartmentEntity.DepartmentName;
				uTOrgDepartment.DutyUser = UTOrgDepartmentEntity.DutyUser;
				uTOrgDepartment.Email = UTOrgDepartmentEntity.Email;
				uTOrgDepartment.Mobile = UTOrgDepartmentEntity.Mobile;
				uTOrgDepartment.OrderIndex = UTOrgDepartmentEntity.OrderIndex;
				uTOrgDepartment.UpdateUser = SessionManager.CurrentSysUser.ID;
				uTOrgDepartment.UpdateDate = DateTime.Now;
				uTOrgDepartment.Remark = UTOrgDepartmentEntity.Remark;
				bizUTOrgDepartment.Update(uTOrgDepartment);
			}


		}
	}


	/// <summary>
	/// 组织机构管理 部门信息管理
	/// 作者：黄剑锋
	/// 时间：2011-10-18
	/// </summary>
	public class ModelSysOrgDepartmentManageDetails : Model
	{
		private IServiceUTOrgDepartment bizUTOrgDepartment;

		/// <summary>
		/// 部门编号
		/// </summary>
		public Guid? UTOrgDepartmentID { get; set; }


		private UTOrgDepartment uTOrgDepartmentEntity;
		/// <summary>
		/// 部门表实体
		/// </summary>
		public UTOrgDepartment UTOrgDepartmentEntity
		{
			get { return uTOrgDepartmentEntity; }
			set { uTOrgDepartmentEntity = value; }
		}

		public void RetriveData()
		{
			var tempExpression = (Expression<Func<UTOrgDepartment, bool>>)(x => x.ID == UTOrgDepartmentID);
			UTOrgDepartmentEntity = bizUTOrgDepartment.GetFirst(tempExpression.ToExpressionNode());
		}

		/// <summary>
		/// 保存修改
		/// </summary>
		public void Save()
		{
			bizUTOrgDepartment.Update(UTOrgDepartmentEntity);
		}
	}


}